package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.DolistBean;
import com.hxkcloud.patient.beans.DolistItemBean;
import com.hxkcloud.patient.beans.NoticeFunctionBean;
import com.hxkcloud.patient.utils.ImageUtil;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableExpandableListView;

import java.util.List;

/**
 * Created by william_sun on 2016-07-09.
 * Description:提醒页，执行单提醒单adapter
 */
public class NoticeDoAdapter extends BaseExpandableListAdapter {

    private PullableExpandableListView elv;
    private List<DolistBean> datas;
    private Context mContext;
    private LayoutInflater mInflater;
    private int listType = 0;//0提醒单 1执行单
    private onDolistFunctionListener listener;

    private final int REFRESH = 222;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH) {
                observer.onChanged();
//                getGroupCount();
//                for (int i = 0; i < getGroupCount(); i++) {
//                    getChildrenCount(i);
//                }
//                notifyDataSetChanged();
//                for (int i = 0; i < datas.size(); i++) {
//                    elv.collapseGroup(i);
//                    elv.expandGroup(i);
//                }
            }
        }
    };

    public void setDatas(List<DolistBean> datas) {
        this.datas = datas;
        handler.sendEmptyMessage(REFRESH);
    }

    public NoticeDoAdapter(Context mContext, PullableExpandableListView lv, int type, List<DolistBean> datas, onDolistFunctionListener listener) {
        this.mContext = mContext;
        this.elv = lv;
        this.listType = type;
        this.datas = datas;
        this.listener = listener;
        mInflater = LayoutInflater.from(mContext);

    }

    private DataSetObserver observer;

    //观察者模式
    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        this.observer = observer;
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        this.observer = null;
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getOrderDetailList().size() + 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getOrderDetailList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return (groupPosition + 1) * (childPosition + 1);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//        if (groupPosition < getGroupCount()) {
        viewholderTitle vhTitle;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_noticedo_title, null);
            vhTitle = new viewholderTitle();
            vhTitle.tv_title = (TextView) convertView.findViewById(R.id.tv_noticedotitle_name);
            vhTitle.iv_arrow = (ImageView) convertView.findViewById(R.id.iv_noticedotitle_arrow);
            convertView.setTag(vhTitle);
        } else {
            vhTitle = (viewholderTitle) convertView.getTag();
        }
        vhTitle.tv_title.setText(datas.get(groupPosition).getOrderName());
        if (isExpanded) {
            vhTitle.iv_arrow.setImageResource(R.mipmap.tab_arrow_up1);
        } else {
            vhTitle.iv_arrow.setImageResource(R.mipmap.tab_arrow1);
        }
        return convertView;
//        } else {
//
//            return new View(mContext);
//        }
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        viewholderInfo vhInfo;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_noticedo_info, null);
            vhInfo = new viewholderInfo();
            vhInfo.ll_func = (LinearLayout) convertView.findViewById(R.id.ll_noticedoinfo_func);
            vhInfo.bt_delete = (Button) convertView.findViewById(R.id.bt_noticedoinfo_delete);
            vhInfo.bt_modify = (Button) convertView.findViewById(R.id.bt_noticedoinfo_modify);
            vhInfo.ll_child = (LinearLayout) convertView.findViewById(R.id.ll_noticedoinfo_child);
            vhInfo.view_line1 = convertView.findViewById(R.id.view_noticedoinfo_line1);
            vhInfo.view_line2 = convertView.findViewById(R.id.view_noticedoinfo_line2);
            vhInfo.ll_reminddate = (LinearLayout) convertView.findViewById(R.id.ll_noticedoinfo_reminddate);
            vhInfo.tv_reminddate = (TextView) convertView.findViewById(R.id.tv_noticedoinfo_reminddate);
            vhInfo.tv_reminddays = (TextView) convertView.findViewById(R.id.tv_noticedoinfo_reminddays);
            vhInfo.ll_items = (LinearLayout) convertView.findViewById(R.id.ll_noticedoinfo_items);
            vhInfo.ll_times1 = (LinearLayout) convertView.findViewById(R.id.ll_noticedoinfo_times1);
            vhInfo.ll_times2 = (LinearLayout) convertView.findViewById(R.id.ll_noticedoinfo_times2);
            convertView.setTag(vhInfo);
        } else {
            vhInfo = (viewholderInfo) convertView.getTag();
        }
        if (childPosition == 0) {
            vhInfo.ll_func.setVisibility(View.VISIBLE);
            vhInfo.ll_child.setVisibility(View.GONE);
            vhInfo.bt_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.delete(groupPosition);
                }
            });
            vhInfo.bt_modify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.modify(groupPosition);
                }
            });
        } else {
            vhInfo.ll_func.setVisibility(View.GONE);
            vhInfo.ll_child.setVisibility(View.VISIBLE);
            childPosition--;
            DolistBean bean = datas.get(groupPosition);
            DolistItemBean item = bean.getOrderDetailList().get(childPosition);

            if (childPosition == 0) {
                vhInfo.view_line1.setVisibility(View.INVISIBLE);
                vhInfo.view_line2.setVisibility(View.VISIBLE);
            } else if (childPosition == bean.getOrderDetailList().size() - 1) {
                vhInfo.view_line1.setVisibility(View.VISIBLE);
                vhInfo.view_line2.setVisibility(View.INVISIBLE);
            } else {
                vhInfo.view_line1.setVisibility(View.VISIBLE);
                vhInfo.view_line2.setVisibility(View.VISIBLE);
            }

            if (listType == 0) {
                vhInfo.view_line1.getLayoutParams().height = ImageUtil.dip2px(25, mContext);
                vhInfo.ll_reminddate.setVisibility(View.GONE);
            } else {
                vhInfo.view_line1.getLayoutParams().height = ImageUtil.dip2px(10, mContext);
                vhInfo.ll_reminddate.setVisibility(View.VISIBLE);
                vhInfo.tv_reminddate.setText(item.getDoOrderDate().split(" ")[0]);
                vhInfo.tv_reminddays.setText("第" + item.getDay() + "天");
            }
            vhInfo.ll_items.removeAllViews();
//            if (!TextUtils.isEmpty(item.getContents())) {
//                TextView tv_content = new TextView(mContext);
//                LinearLayout.LayoutParams lp_tv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                tv_content.setLayoutParams(lp_tv);
//                tv_content.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
//                tv_content.setTextSize(16);
//                tv_content.setText(item.getContents());
//                vhInfo.ll_items.addView(tv_content);
//            }
//            for (NoticeFunctionBean function : item.getCheckIds()) {
//                TextView tv_function = new TextView(mContext);
//                LinearLayout.LayoutParams lp_function = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                tv_function.setLayoutParams(lp_function);
//                tv_function.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
//                //http://blog.sina.com.cn/s/blog_671d2e4f0101f8f7.html
//                tv_function.setTextSize(16);
//                tv_function.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
//                tv_function.setText(function.getMonitorName());
//                vhInfo.ll_items.addView(tv_function);
////                tv_function.setTag(function.getMonitorId());
////                tv_function.setOnClickListener(new View.OnClickListener() {
////                    @Override
////                    public void onClick(View v) {
////                        listener.check((String) v.getTag());
////                    }
////                });
//            }
//            for (DolistArticleBean article : item.getArticleIds()) {
//                TextView tv_article = new TextView(mContext);
//                LinearLayout.LayoutParams lp_tv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                tv_article.setLayoutParams(lp_tv);
//                tv_article.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
//                tv_article.setTextSize(16);
//                tv_article.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
//                tv_article.setText(article.getArticleName());
//                vhInfo.ll_items.addView(tv_article);
//                tv_article.setTag(article.getArticleId());
//                tv_article.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        listener.article((String) v.getTag());
//                    }
//                });
//            }
            for (NoticeFunctionBean f : item.getItemList()) {
                TextView tv_content = new TextView(mContext);
                LinearLayout.LayoutParams lp_tv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp_tv.bottomMargin = ImageUtil.dip2px(5, mContext);
                tv_content.setLayoutParams(lp_tv);

                tv_content.setTextSize(16);
                tv_content.setText(f.getContent());
                if (!f.getType().equals("2")) {
                    tv_content.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                    tv_content.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                } else {
                    tv_content.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                }
                if (f.getType().equals("1")) {
                    tv_content.setTag(f.getItemId());
                    tv_content.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            listener.article((String) v.getTag());
                        }
                    });
                } else {
                    tv_content.setOnClickListener(null);
                }
                vhInfo.ll_items.addView(tv_content);
            }
            vhInfo.ll_times1.removeAllViews();
            vhInfo.ll_times2.removeAllViews();
            for (int i = 0; i < Math.min(4, item.getTimeIds().size()); i++) {
                String time = item.getTimeIds().get(i).getTime();
                TextView tv_time = new TextView(mContext);
                LinearLayout.LayoutParams lp_time = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp_time.rightMargin = ImageUtil.dip2px(5, mContext);
                tv_time.setLayoutParams(lp_time);
                tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                tv_time.setTextSize(14);
                tv_time.setBackgroundResource(R.drawable.foundation_grey_halfcircle);
                tv_time.setPadding(ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext));
                tv_time.setText(time);
                vhInfo.ll_times1.addView(tv_time);
            }
            if (item.getTimeIds().size() > 4) {
                vhInfo.ll_times2.setVisibility(View.VISIBLE);
                for (int i = 4; i < item.getTimeIds().size(); i++) {
                    String time = item.getTimeIds().get(i).getTime();
                    TextView tv_time = new TextView(mContext);
                    LinearLayout.LayoutParams lp_time = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp_time.rightMargin = ImageUtil.dip2px(5, mContext);
                    tv_time.setLayoutParams(lp_time);
                    tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                    tv_time.setTextSize(14);
                    tv_time.setBackgroundResource(R.drawable.foundation_grey_halfcircle);
                    tv_time.setPadding(ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext));
                    tv_time.setText(time);
                    vhInfo.ll_times2.addView(tv_time);
                }
            } else {
                vhInfo.ll_times2.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    private class viewholderTitle {
        TextView tv_title;
        ImageView iv_arrow;
    }

    private class viewholderInfo {
        LinearLayout ll_func;
        Button bt_delete;
        Button bt_modify;
        LinearLayout ll_child;
        View view_line1;
        View view_line2;
        LinearLayout ll_reminddate;
        TextView tv_reminddate;
        TextView tv_reminddays;
        LinearLayout ll_items;
        LinearLayout ll_times1;
        LinearLayout ll_times2;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return childId;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return groupId;
    }

    public interface onDolistFunctionListener {
//        void check(String id);//监测,护士不能操作，护士看到要去通知患者

        void article(String id);//文章

        void delete(int pIndex);//删除order

        void modify(int pIndex);//修改order
    }
}
